﻿/* 
This file is part of Ingo Karstein's Excel Export project

**Do not remove this comment**

Please see the project homepage at CodePlex:
  http://spexcelexport.codeplex.com/

Please see my blog:
  http://ikarstein.wordpress.com

Licence: GPLv2
    Ingo Karstein's SharePoint 2010 Custom Ribbon Demo
    Copyright (C) 2011  Ingo Karstein, ikarstein@hotmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ik.SharePoint2010.ExcelExport
{
    internal class StringTable
    {
        private SortedList<string, int> _strings;

        public StringTable()
        {
            _strings = new SortedList<string, int>();
        }

        public int GetId(string s)
        {
            if( _strings.ContainsKey(s) )
                return _strings[s];
            else
            {
                int newId = _strings.Count;
                _strings.Add(s, newId);
                return newId;
            }
        }

        public string ToXmlString()
        {
            SortedList<int, string> _sortedById = new SortedList<int,string>();
            foreach(var i in _strings)
            {
                _sortedById.Add(i.Value, i.Key);
            };

            StringBuilder sb = new StringBuilder();
            sb.AppendLine(@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?> 
                            <sst xmlns=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"" count="""+_strings.Count.ToString()+
                          @""" uniqueCount="""+_strings.Count.ToString()+ @""">");

            foreach( var j in _sortedById)
            {
                string tmpl = @"
<si>
  <t>{0}</t> 
</si>
";
                sb.AppendLine(string.Format(tmpl, j.Value));
            };

            sb.AppendLine(@"</sst>");

            return sb.ToString();
        }
    }
}
